package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Discovery;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.WishProduct;
import com.simon.mengkou.data.pref.OuerPreferences;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.activity.HotSaleActivity;
import com.simon.mengkou.ui.activity.ProductDetailActivity;
import com.simon.mengkou.ui.activity.WishListActivity;
import com.simon.mengkou.ui.view.HorizontalListView;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiazhenshui on 16/7/21.
 * 心愿单视频器
 */

public class HotSaleAdapter extends AbsAdapter<WishProduct> {
    private HotSaleActivity mActivity;

    public HotSaleAdapter(HotSaleActivity context) {
        super(context);
        this.mActivity = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_hot_sale_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final WishProduct data = getItem(position);

        holder.mLlRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OuerDispatcher.presentProductDetailActivity(mContext, data.getId());
            }
        });
        //商品图片
        OuerApplication.mImageLoader.loadImage(holder.mSdvImage, data.getImage().getUrl());


        //商品标题
        holder.mTvTitle.setText(data.getTitle());
        //价格
        holder.mTvPrice.setText(mContext.getString(R.string.common_price,
                UtilOuer.formatPrice(data.getPrice())));
        //是否加入购物车
        if(data.isInCart()) {
            holder.mBtnCart.setText(R.string.wish_in_cart);
            holder.mBtnCart.setEnabled(false);
        } else {
            holder.mBtnCart.setText(R.string.wish_not_in_cart);
            holder.mBtnCart.setEnabled(true);

            holder.mBtnCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addCart(data);
                }
            });
        }

        //排名
        holder.mTvTop.setText(mContext.getString(R.string.hot_sale_top, position+1));
        //销量
        holder.mTvNum.setText(mContext.getString(R.string.hot_sale_num, data.getWishCount()));

        return convertView;
    }

    public void addCart(final WishProduct data) {
        AgnettyFuture future = OuerApplication.mOuerFuture.cartAdd(
                data.getId(), 1,
                new OuerFutureListener(mContext) {
                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);
                        mActivity.setWaitingDialog(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        mActivity.setWaitingDialog(false);
                        Boolean flag = (Boolean) result.getAttach();
                        data.setInCart(flag);

                        if(flag) {
                            notifyDataSetChanged();
                            UtilOuer.toast(mContext, R.string.product_add_cart_success);
                        } else {
                            UtilOuer.toast(mContext, R.string.product_add_cart_failure);
                        }
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        mActivity.setWaitingDialog(false);
                        UtilOuer.showExceptionToast(mContext, result, R.string.product_add_cart_failure);
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        mActivity.setWaitingDialog(false);
                    }
                });

        mActivity.attachDestroyFutures(future);
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //
        @Bind(R.id.wish_id_root)
        LinearLayout mLlRoot;
        //商品图片
        @Bind(R.id.wish_id_image)
        SimpleDraweeView mSdvImage;
        //商品标题
        @Bind(R.id.wish_id_title)
        TextView mTvTitle;
        //商品价格
        @Bind(R.id.wish_id_price)
        TextView mTvPrice;
        //商品标题
        @Bind(R.id.wish_id_cart)
        Button mBtnCart;
        //商品排名
        @Bind(R.id.wish_id_top)
        TextView mTvTop;
        //商品销量
        @Bind(R.id.wish_id_num)
        TextView mTvNum;
    }
}
